<?php
/**
 * 商品发布
 * Created by PhpStorm.
 * User: Wang YuLong
 * Date: 2018/11/8
 * Time: 10:23
 */
namespace app\index\controller;

use think\Controller;
use think\Image;
use app\index\model\Issue;
use app\index\model\Brand;
use app\index\model\Classify;
use app\index\model\Details;

class Issues extends Comm
{
    /**
     * 商品发布的首页
     */
     public function commodity()
     {
         $re=new Issue ;
         $re =$re->commodity();
         return view('commodity',['commodity'=>$re,'i'=>1]);
     }

    /**
     * 发布新的商品
     * @return \think\response\View
     */
    public function add()
    {
        if(isset($_POST['submit']))
        {
            $data = $_POST;
            $image=request()->file('image');
            $issue = new Issue();
            $info = $issue->adddo($data,$image);
            if($info===1)
            {
                zsgc('添加了新的商品信息，名称为'.input('name'));
                $this->success('操作成功','index/issues/commodity');
            }else
            {
                $this->error('操作失败','index/issues/commodity');
            }
        }else{
            //代理商id
            $brandid = Brand::all()->toArray();
            //商品分类id
            $classifyid = Classify::all()->toArray();

            $this->assign('brandid',$brandid);
            $this->assign('classifyid',$classifyid);
            return view('add');
        }
    }

    /**
     * 删除商品信息
     * @throws \think\exception\DbException
     */
    public function del()
    {
        $id = trim(input('id'));
        $data = Issue::get($id);
        $path=ROOT_PATH . 'public' . DS . 'static' . DS . 'img_issue'. DS .$data['photo'];
        if(file_exists($path)){
            @unlink($path);
            clearstatcache();
        }
        zsgc('删除了一个商品信息，名称为'.$data['name']);
        $re=Issue::destroy($id);
        if($re==false){
            show(0,'商品删除失败');
        }else{
            show(1,'商品删除成功');
        }
    }

    /**
     * 修改商品信息
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function mod()
    {

        if(isset($_POST['submit']))
        {
            $data = $_POST;
            $id = trim($data['id']);
            $image=request()->file('image');
            $issue = new Issue();
            $data = $issue->moddo($data,$image,$id);
            if ($data===1)
            {
                zsgc('修改了名为'.input('name').'的商品信息');
                $this->success('操作成功','index/issues/commodity');
            }else
            {
                $this->error('操作失败','index/issues/commodity');
            }
        }else{
            $id = trim(input('id'));
            $issue = new Issue();
            $data = $issue->mod($id);
            //品牌id
            $brandid = Brand::all()->toArray();
            //商品分类id
            $classifyid = Classify::all()->toArray();
            return view('mod',['dataa'=>$data,'brandid'=>$brandid,'classifyid'=>$classifyid]);
        }
    }

    /**
     * 商品详情图片的显示
     * @return \think\response\View
     */
    public function details()
    {
        $id = input('id');
        $details = new Details();
        $data = $details->index($id);
        return view('details',['data'=>$data,'id'=>$id]);
    }

    /**
     * 修改商品详情图片的序号
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function modsorts()
    {
        $id=input('ids');
        $sort=input('ID');
        $rs=db('details')
            ->where('id',$id)
            ->update(['sort' =>$sort]);
        if($rs==1)
        {
            zsgc('修改了一个商品详情的序号，修改后的序号为'.$sort);
            return "1";
        }
    }

    /**
     * 删除一张商品详情图片
     * @throws \think\exception\DbException
     */
    public function deldetails()
    {
        $ids = input('ids');
        $id = input('id');
        $data = Details::get($id);
        $path=ROOT_PATH . 'public' . DS . 'static' . DS . 'img_issue'. DS .$data['url'];
        if(file_exists($path)){
            @unlink($path);
            clearstatcache();
        }
        $re=Details::destroy($id);
        if($re==false){
            $this->error('商品详情图片删除失败','index/issues/details?id='.$ids);
        }else{
            zsgc('删除了商品信息的图片');
            $this->success('商品详情图片删除成功','index/issues/details?id='.$ids);
        }
    }

    /**
     * 添加新的商品详情
     * @return \think\response\View
     */
    public function adddetails()
    {
        $id = input('id');
        if (isset($_POST['submit']))
        {
            $id = input('id');
            $image=request()->file('image');

            $details = new Details();
            $info = $details->adddo($id,$image);
            if($info===1)
            {
                zsgc('添加新的商品详情');
                $this->success('操作成功','index/issues/details?id='.$id);
            }else
            {
                $this->error('操作失败','index/issues/details?id='.$id);
            }
        }else
        {
            return view('adddetails',['id'=>$id]);
        }
    }
    //预售商品信息
    public function preindex(){
        $re=new Issue ;
        $re =$re->PreData();
        return view('preindex',['commodity'=>$re,'i'=>1]);
    }
}
